const fs = require('fs');
const path = require('path');
const uploadController = require('../controller/upload.js');
async function handlerRouter(req, res) {
    if (req.url === '/') {
        const page = fs.readFileSync(path.resolve(__dirname, '../assets/index.html'), {
            encoding: "utf8",
        });
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write(page);
        res.end();
    } else if (req.url === '/favicon.ico') {
        const favicon = fs.readFileSync(path.resolve(__dirname, '../favicon.ico'));
        res.writeHead(200, { 'Content-Type': 'image/x-icon' });
        res.write(favicon);
        res.end();
    } else if (req.url === '/css/index.css') {
        const cssTxt = fs.readFileSync(path.resolve(__dirname, '../assets/css/index.css'), {
            encoding: "utf8",
        });
        res.writeHead(200, { 'Content-Type': 'text/css' });
        res.write(cssTxt);
        res.end();
    }else if (req.url === '/merge.jpg') {
        const cssTxt = fs.readFileSync(path.resolve(__dirname, '../assets/images/merge.jpg'));
        res.writeHead(200, { 'Content-Type': 'image/jpeg' });
        res.write(cssTxt);
        res.end();
    }else if (req.url === '/test.jpg') {
        const cssTxt = fs.readFileSync(path.resolve(__dirname, '../assets/images/test.jpg'));
        res.writeHead(200, { 'Content-Type': 'image/jpeg' });
        res.write(cssTxt);
        res.end();
    }else if (req.url === '/main.js') {
        const ajs = fs.readFileSync(path.resolve(__dirname, "../assets/js/main.js"), {
            encoding: "utf8",
        });
        res.writeHead(200, { "Content-Type": "text/js" });
        res.write(ajs);
        res.end();
    } else if (req.url === '/spark-md5.js') {
        const ajs = fs.readFileSync(path.resolve(__dirname, "../assets/js/spark-md5.js"), {
            encoding: "utf8",
        });
        res.writeHead(200, { "Content-Type": "text/js" });
        res.write(ajs);
        res.end();
    } else if (req.url === '/upload') {
        res.setHeader('Content-Type', 'application/json');
        const result = await uploadController(req, res);
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
        res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
        res.end(JSON.stringify(result));
    }
}
module.exports = handlerRouter;